********************************************************************************
* Replication material for "The emotional effect of terrorism"
* Scientific Reports
* Vincenzo Bove, Georgios Efthyvoulou, Armine Ghazaryan & Harry Pickard

********************************************************************************
// Pre-amble

clear all

*** Set working directory to root folder here ***
cd ""

glo data "data"
glo output "output"

global tweetlevel "retweet_count reply_count like_count quote_count"
global emotions Negative Fear Anger Sadness Disgust


********************************************************************************
// Replication code



***************************
**********TABLE 1**********
***************************

use "$data/twitter_data.dta", clear

preserve

*Table 1 

collapse $tweetlevel  Negative Fear Anger Sadness Disgust attackN (max) post_attack hour dummy48, by(datetime author_id )

foreach x in $tweetlevel{
gen `x'1=round(`x')
drop `x'
rename `x'1 `x'
}


label var post_attack "Post attack" 
label var dummy48 "24hr-bandwidth"

foreach x of global emotions{
quietly reghdfe `x' post_attack $tweetlevel , absorb(author_id##attackN hour, save)  cluster(author_id)
est store reg1`x'

egen mean_`x'=mean(`x') if post_attack==0 & e(sample)
ereplace mean_`x'=max(mean_`x')
estadd scalar ymean = mean_`x', replace: reg1`x'
drop mean_`x'
}

estadd scalar N_users = e(N_clust), replace :  reg1Negative reg1Fear reg1Anger reg1Sadness reg1Disgust


estout   reg1Negative reg1Fear reg1Anger reg1Sadness reg1Disgust using "$output/reg_maintable.tex", cells("b(star label( )  fmt(3))" " se(par label( ) fmt(3))") ///
replace mlabels(none, none) starlevels(* 0.10 ** 0.05 *** 0.01) stats( ymean N N_users, labels("Pre-attack dependent variable" "Observations" "Number of users")  fmt("%7.3fc" "%9.0fc" "%7.0fc") ) collabels(none, none)  margin  ///
label msign (--) nobaselevels unstack noomitted  ///									
style(tex)   drop(_cons $tweetlevel  ) ///		
prehead("\begin{table*}[T]\begin{center} \begin{minipage}{1\linewidth}" ///
"\footnotesize \caption{The emotional effect of terrorism: baseline results} \label{reg1} " ///
"{\begin{threeparttable}\begin{tabular}{ l  c c c c c }  " ///
"\toprule & Negative & Fear & Anger  & Sadness & Disgust \\ " ///
" & (1) & (2) & (3)  & (4) & (5)   \\ \hline "  /// 
"& \\" /// 
" \multicolumn{6}{l}{\textit{Panel A: Simple specification }}  \\  " ///
"&  \\" ) ///
prefoot("\cline{2-6}  ")

***

foreach x of global emotions{

quietly reghdfe `x' post_attack##dummy48   $tweetlevel, absorb(author_id##attackN hour , save)  cluster(author_id)
est store reg1`x'
egen mean_`x'=mean(`x') if post_attack==0 & e(sample)
ereplace mean_`x'=max(mean_`x')
estadd scalar ymean = mean_`x', replace: reg1`x'

drop mean_`x'

}

estadd scalar N_users = e(N_clust), replace :  reg1Negative reg1Fear reg1Anger reg1Sadness reg1Disgust


estout   reg1Negative reg1Fear reg1Anger reg1Sadness reg1Disgust using "$output/reg_maintable.tex", cells("b(star label( )  fmt(3))" " se(par label( ) fmt(3))") ///
append mlabels(none, none) starlevels(* 0.10 ** 0.05 *** 0.01) stats( ymean N N_users, labels("Pre-attack dependent variable (mean)" "Observations" "Number of users")   fmt("%7.3fc" "%9.0fc" "%7.0fc") ) collabels(none, none)  margin discrete("") stardrop() ///
label msign (--) nobaselevels unstack noomitted  ///									
style(tex)   drop(_cons  $tweetlevel ) ///		
prehead(" \hline & \\" ///
"\multicolumn{6}{l}{\textit{Panel B: Interaction with a 24-hour bandwidth}}  \\  " ///
"&  \\" ) ///
prefoot(" \cline{2-6}  ") ///
postfoot( " \bottomrule" ///
" \end{tabular} \end{threeparttable} }\end{minipage}\end{center} \end{table*}" )

restore

***************************
**********TABLE 2**********
***************************

preserve

keep if terrorplus_related==0

collapse $tweetlevel Negative Fear Anger Sadness Disgust attackN (max) post_attack hour dummy48, by(datetime author_id)

foreach x in $tweetlevel{
gen `x'1=round(`x')
drop `x'
rename `x'1 `x'
}

label var post_attack "Post attack" 
label var dummy48 "24hr-bandwidth"

foreach x of global emotions{

quietly reghdfe `x' post_attack##dummy48  $tweetlevel , absorb(author_id#attackN hour, save) vce(cluster author_id)
est store reg1`x'

}

estout  reg1Negative reg1Fear reg1Anger reg1Sadness reg1Disgust using "$output/reg_nonterror.tex", cells("b(star label( )  fmt(3))" " se(par label( ) fmt(3))") ///
replace mlabels(none, none) starlevels(* 0.10 ** 0.05 *** 0.01) stats( N , labels("Non-terror-related obs.")  fmt("%9.0fc")  ) collabels(none, none)  margin discrete("") stardrop($covr ) ///
label msign (--) nobaselevels unstack noomitted  ///									
style(tex)   keep(*post_attack*) ///		
prehead("\begin{table}\centering \footnotesize " ///
"{\begin{threeparttable} " ///
" \caption{The emotional effect of terrorism: non-terror-related tweets} \label{reg2} " ///
"\begin{tabular}{ l  c c c c c }  " ///
"\toprule & Negative & Fear & Anger  & Sadness & Disgust \\ " ///
" & (1) & (2) & (3)  & (4) & (5)   \\ \hline "  /// 
"& \\"  ) ///
prefoot("  \cline{2-6} " ) ///
postfoot( " \bottomrule" ///
" \end{tabular} \end{threeparttable} }\end{minipage}\end{center} \end{table*}" )

restore


****************************
**********FIGURE 2**********
****************************

preserve

collapse $tweetlevel Negative Fear Anger Sadness Disgust  attackN (max) post_attack hour muslim_extremism highcasualties highcoverage dummy48, by(datetime author_id )

foreach x in $tweetlevel{
gen `x'1=round(`x')
drop `x'
rename `x'1 `x'
}

 foreach x of global emotions{

qui reghdfe `x' post_attack##dummy48  $tweetlevel if muslim_extremism==0, absorb(author_id#attackN hour  , save)  cluster(author_id)
eststo Farright`x': qui margins, expression(_b[1.post_attack] + _b[1.post_attack#1.dummy48]) post

qui reghdfe `x' post_attack##dummy48  $tweetlevel if muslim_extremism==1, absorb(author_id#attackN hour , save)  cluster(author_id)
eststo Muslim`x': qui margins, expression(_b[1.post_attack] + _b[1.post_attack#1.dummy48]) post

qui  reghdfe `x' post_attack##dummy48  $tweetlevel if highcasualties==1, absorb(author_id#attackN hour , save)  cluster(author_id)
eststo Highcasualty`x': qui margins, expression(_b[1.post_attack] + _b[1.post_attack#1.dummy48]) post

qui  reghdfe `x' post_attack##dummy48  $tweetlevel if highcasualties==0, absorb(author_id#attackN hour , save)  cluster(author_id)
eststo Lowcasualty`x': qui margins, expression(_b[1.post_attack] + _b[1.post_attack#1.dummy48]) post

qui  reghdfe `x' post_attack##dummy48  $tweetlevel if highcoverage==1, absorb(author_id#attackN hour, save)  cluster(author_id)
eststo Highmedia`x': qui margins, expression(_b[1.post_attack] + _b[1.post_attack#1.dummy48]) post

qui  reghdfe `x' post_attack##dummy48 $tweetlevel if highcoverage==0, absorb(author_id#attackN hour , save)  cluster(author_id)
eststo Lowmedia`x': qui margins, expression(_b[1.post_attack] + _b[1.post_attack#1.dummy48]) post
}

coefplot (FarrightNegative MuslimNegative, color(navy) ciopts(color(navy)))  (LowcasualtyNegative HighcasualtyNegative, color(navy) ciopts(color(navy)))  (LowmediaNegative HighmediaNegative, color(navy) ciopts(color(navy))) , xline( 0 ,  lpattern(solid) lcolor(red) ) horizontal   legend(off) grid(none ) aseq swapnames order(FarrightNegative: MuslimNegative:   LowcasualtyNegative: HighcasualtyNegative:  LowmediaNegative: HighmediaNegative:)   levels(95) coeflabels( FarrightNegative="Far-right" MuslimNegative="Islamic" LowcasualtyNegative="Low" HighcasualtyNegative="High" LowmediaNegative="Low" HighmediaNegative="High",  notick labsize(medlarge) labcolor(navy) labgap(3)) headings(FarrightNegative = "{bf:By perpetrator}" LowcasualtyNegative = "{bf: By number of victims}"  LowmediaNegative = "{bf: By media coverage}" , labcolor(cranberry) labsize(medlarge) )  graphr(style(none) color(white) ) title("Negative", size(large)) xlabels( #6, labsize(medlarge) ) ///
xline(0.01 ,  lpattern(shortdash ) lcolor(gs5) ) xscale(range(0 0.025))
graph save heterogeneity_Negative, replace

coefplot (FarrightFear MuslimFear, color(navy) ciopts(color(navy)))  (LowcasualtyFear HighcasualtyFear, color(navy) ciopts(color(navy)))  (LowmediaFear HighmediaFear, color(navy) ciopts(color(navy))) ,  xline( 0 ,  lpattern(solid) lcolor(red) ) horizontal   legend(off) grid(none ) aseq swapnames order(FarrightFear: MuslimFear:   LowcasualtyFear: HighcasualtyFear:  LowmediaFear: HighmediaFear:)   levels(95) coeflabels( FarrightFear="Far-right" MuslimFear="Islamic" LowcasualtyFear="Low" HighcasualtyFear="High" LowmediaFear="Low" HighmediaFear="High",  notick labsize(medlarge) labcolor(navy) labgap(3)) headings(FarrightFear = "{bf:By perpetrator}"  LowcasualtyFear = "{bf: By number of victims}"  LowmediaFear = "{bf: By media coverage}" , labcolor(cranberry) labsize(medlarge))  graphr(style(none) color(white) ) title("Fear", size(large)) xlabels( #6, labsize(medlarge) ) ///
xline( 0.014 ,  lpattern(shortdash ) lcolor(gs5) ) xscale(range(0 0.025)) 
graph save heterogeneity_Fear, replace

coefplot (FarrightAnger MuslimAnger, color(navy) ciopts(color(navy)))  (LowcasualtyAnger HighcasualtyAnger, color(navy) ciopts(color(navy)))  (LowmediaAnger HighmediaAnger, color(navy) ciopts(color(navy))) ,  xline( 0 ,  lpattern(solid) lcolor(red) ) horizontal   legend(off) grid(none ) aseq swapnames order(FarrightAnger: MuslimAnger:   LowcasualtyAnger: HighcasualtyAnger:  LowmediaAnger: HighmediaAnger:)   levels(95) coeflabels( FarrightAnger="Far-right" MuslimAnger="Islamic" LowcasualtyAnger="Low" HighcasualtyAnger="High" LowmediaAnger="Low" HighmediaAnger="High",  notick labsize(medlarge) labcolor(navy) labgap(3)) headings(FarrightAnger = "{bf:By perpetrator}"  LowcasualtyAnger = "{bf: By number of victims}"   LowmediaAnger = "{bf: By media coverage}" , labcolor(cranberry) labsize(medlarge))  graphr(style(none) color(white) ) title("Anger", size(large)) xlabels( #6, labsize(medlarge) ) ///
xline(0.009 ,  lpattern(shortdash ) lcolor(gs5) ) xscale(range(0 0.025))
graph save heterogeneity_Anger, replace

coefplot (FarrightSadness MuslimSadness, color(navy) ciopts(color(navy)))  (LowcasualtySadness HighcasualtySadness, color(navy) ciopts(color(navy)))  (LowmediaSadness HighmediaSadness, color(navy) ciopts(color(navy))) ,  xline( 0 ,  lpattern(solid) lcolor(red) ) horizontal   legend(off) grid(none ) aseq swapnames order(FarrightSadness: MuslimSadness:   LowcasualtySadness: HighcasualtySadness:  LowmediaSadness: HighmediaSadness:)   levels(95) coeflabels( FarrightSadness="Far-right" MuslimSadness="Islamic" LowcasualtySadness="Low" HighcasualtySadness="High" LowmediaSadness="Low" HighmediaSadness="High",  notick labsize(medlarge) labcolor(navy) labgap(3)) headings(FarrightSadness = "{bf:By perpetrator}"  LowcasualtySadness = "{bf: By number of victims}"  LowmediaSadness = "{bf: By media coverage}" , labcolor(cranberry) labsize(medlarge))  graphr(style(none) color(white) ) title("Sadness", size(large)) xlabels( #6, labsize(medlarge) ) ///
xline(0.005 ,  lpattern(shortdash ) lcolor(gs5) ) xscale(range(0 0.025))
graph save heterogeneity_Sadness, replace

coefplot (FarrightDisgust MuslimDisgust, color(navy) ciopts(color(navy)))  (LowcasualtyDisgust HighcasualtyDisgust, color(navy) ciopts(color(navy)))  (LowmediaDisgust HighmediaDisgust, color(navy) ciopts(color(navy))) ,  xline( 0 ,  lpattern(solid) lcolor(red) ) horizontal   legend(off) grid(none ) aseq swapnames order(FarrightDisgust: MuslimDisgust:   LowcasualtyDisgust: HighcasualtyDisgust:  LowmediaDisgust: HighmediaDisgust:)   levels(95) coeflabels( FarrightDisgust="Far-right" MuslimDisgust="Islamic" LowcasualtyDisgust="Low" HighcasualtyDisgust="High" LowmediaDisgust="Low" HighmediaDisgust="High",  notick labsize(medlarge) labcolor(navy) labgap(3)) headings(FarrightDisgust = "{bf:By perpetrator}"  LowcasualtyDisgust = "{bf: By number of victims}"  LowmediaDisgust = "{bf: By media coverage}" , labcolor(cranberry) labsize(medlarge))  graphr(style(none) color(white) ) title("Disgust", size(large)) xlabels( #6, labsize(medlarge) ) ///
xline(0.005 ,  lpattern(shortdash ) lcolor(gs5) ) xscale(range(0 0.025))
graph save heterogeneity_Disgust, replace

graph combine heterogeneity_Negative.gph  heterogeneity_Fear.gph heterogeneity_Anger.gph heterogeneity_Sadness.gph heterogeneity_Disgust.gph  , rows(3)  iscale(*.75) graphr(style(none) color(white) ) xcommon  holes(2) 
graph export "$output/Heterogeneity.pdf", replace

restore

****************************
**********FIGURE 1**********
****************************


use "$data/hrs_attack.dta", clear

foreach x of global emotions {

twoway line b_`x'_ma3_before event_time, msize(small) color(blue)  || line b_`x'_ma3_after event_time, msize(small) color(red) || rarea ll_`x'_ma3_before ul_`x'_ma3_before event_time, lcolor(blue%0) fcolor(blue%20)  || rarea ll_`x'_ma3_after ul_`x'_ma3_after event_time, lcolor(red%0) fcolor(red%20)  xline(92, lpattern(shortdash) lcolor(black)) ylabel(,  labsize(medsmall) labcolor(black) labgap(1) angle(0)  nogrid) xtitle("", size(small) color(black) ) title(`x', color(black)) graphr(style(none) color(white) ) legend(order(1 "Pre-attack" 2 "Post-attack" 4 "95% CI" ) size(small) region(style(none)) r(1)) xlabel(minmax 69(6)117 92 "-1",  valuelabel  labsize(medsmall) labcolor(black) labgap(1) angle(0)) yline(0, lcolor(black))

graph save `x', replace
}


grc1leg Negative.gph Fear.gph Anger.gph Sadness.gph Disgust.gph  , rows(3)  b1("Hours relative to attack", size(small) color(black) )   iscale(*.9) graphr(style(none) color(white) ) ring(2) ycommon  holes(2)
graph export "$output/time_event.pdf", replace

foreach x of global emotions {
erase `x'.gph 
}


***************************
**********TABLE 3**********
***************************

use "$data/twitter_data.dta", clear

keep if migration_related==1

collapse $tweetlevel Negative Fear Anger Sadness Disgust muslim_extremism  attackN (max) post_attack hour dummy48, by(datetime author_id )

foreach x in $tweetlevel{
gen `x'1=round(`x')
drop `x'
rename `x'1 `x'
}

label var post_attack "Post attack" 
label var dummy48 "24hr-bandwidth"

foreach x of global emotions{
quietly reghdfe `x' post_attack##dummy48  $tweetlevel, absorb(attackN hour  , save)  cluster(author_id)
est store reg1`x'

egen mean_`x'=mean(`x') if post_attack==0 & e(sample)
ereplace mean_`x'=max(mean_`x')
estadd scalar ymean = mean_`x', replace: reg1`x'
drop mean_`x'

}

estadd scalar N_users = e(N_clust), replace :  reg1Negative reg1Fear reg1Anger reg1Sadness reg1Disgust

estout  reg1Negative reg1Fear reg1Anger reg1Sadness reg1Disgust using "$output/reg_migr.tex", cells("b(star label( )  fmt(3))" " se(par label( ) fmt(3))" ) ///
replace mlabels(none, none) starlevels(* 0.10 ** 0.05 *** 0.01) stats( ymean N N_users, labels("Pre-attack dependent variable" "Observations" "Number of users")  fmt("%7.3fc" "%9.0fc" "%7.0fc") ) collabels(none, none)  margin  ///
label msign (--) nobaselevels unstack noomitted  ///									
style(tex)  keep(*post_attack*)  ///		
prehead("\begin{table*}[T]\begin{center} \begin{minipage}{1\linewidth}" ///
"\footnotesize \caption{The effect of terror attacks on emotional affects of migration-related tweets} \label{reg1migr} " ///
"{\begin{threeparttable}\begin{tabular}{ l  c c c c c  }  " ///
"\toprule & Negative & Fear & Anger  & Sadness & Disgust \\ " ///
" & (1) & (2) & (3)  & (4) & (5)   \\ \hline "  /// 
"& \\" /// 
" \multicolumn{6}{l}{\textit{Panel A: All attacks; includes attack FEs }} \\  " ///
"&  \\" ) ///
prefoot(" \cline{2-6}  ") 

***

preserve 

keep if muslim_extremism==1

foreach x of global emotions{
quietly reghdfe `x' post_attack##dummy48  $tweetlevel, absorb(attackN hour  , save)  cluster(author_id)
est store reg1`x'

egen mean_`x'=mean(`x') if post_attack==0 & e(sample)
ereplace mean_`x'=max(mean_`x')
estadd scalar ymean = mean_`x', replace: reg1`x'
drop mean_`x'

}

estadd scalar N_users = e(N_clust), replace : reg1Negative reg1Fear reg1Anger reg1Sadness reg1Disgust

estout  reg1Negative reg1Fear reg1Anger reg1Sadness reg1Disgust using "$output/reg_migr.tex", cells("b(star label( )  fmt(3))" " se(par label( ) fmt(3))") ///
append mlabels(none, none) starlevels(* 0.10 ** 0.05 *** 0.01) stats( ymean N N_users, labels("Pre-attack dependent variable" "Observations" "Number of users")  fmt("%7.3fc" "%9.0fc" "%7.0fc") ) collabels(none, none)  margin  ///
label msign (--) nobaselevels unstack noomitted  ///									
style(tex)  keep(*post_attack*)  ///		
prehead(" \hline & \\" ///
"\multicolumn{6}{l}{\textit{Panel B: Islamic attacks, includes attack FEs }} \\  " ///
"&  \\" ) ///
prefoot(" \cline{2-6}  ")

restore 

***
foreach x of global emotions{
quietly reghdfe `x' post_attack##dummy48  $tweetlevel, absorb(author_id#attackN hour  , save)  cluster(author_id)
est store reg1`x'

egen mean_`x'=mean(`x') if post_attack==0 & e(sample)
ereplace mean_`x'=max(mean_`x')
estadd scalar ymean = mean_`x', replace: reg1`x'
drop mean_`x'
}

estadd scalar N_users = e(N_clust), replace : reg1Negative reg1Fear reg1Anger reg1Sadness reg1Disgust


estout  reg1Negative reg1Fear reg1Anger reg1Sadness reg1Disgust using "$output/reg_migr.tex", cells("b(star label( )  fmt(3))" " se(par label( ) fmt(3))") ///
append mlabels(none, none) starlevels(* 0.10 ** 0.05 *** 0.01) stats( ymean N N_users, labels("Pre-attack dependent variable" "Observations" "Number of users")  fmt("%7.3fc" "%9.0fc" "%7.0fc") ) collabels(none, none)  margin  ///
label msign (--) nobaselevels unstack noomitted  ///									
style(tex)  keep(*post_attack*) ///		
prehead(" \hline & \\" ///
"\multicolumn{6}{l}{\textit{Panel C: All attacks; includes individual $\times$ attack FEs }} \\  " ///
"&  \\" ) ///
prefoot("  \cline{2-6} ")


***

preserve 

keep if muslim_extremism==1

foreach x of global emotions{
quietly reghdfe `x' post_attack##dummy48  $tweetlevel, absorb(author_id#attackN hour  , save)  cluster(author_id)
est store reg1`x'

egen mean_`x'=mean(`x') if post_attack==0 & e(sample)
ereplace mean_`x'=max(mean_`x')
estadd scalar ymean = mean_`x', replace: reg1`x'
drop mean_`x'
}

estadd scalar N_users = e(N_clust), replace : reg1Negative reg1Fear reg1Anger reg1Sadness reg1Disgust

estout  reg1Negative reg1Fear reg1Anger reg1Sadness reg1Disgust using "$output/reg_migr.tex", cells("b(star label( )  fmt(3))" " se(par label( ) fmt(3))") ///
append mlabels(none, none) starlevels(* 0.10 ** 0.05 *** 0.01) stats( ymean N N_users, labels("Pre-attack dependent variable" "Observations" "Number of users")  fmt("%7.3fc" "%9.0fc" "%7.0fc") ) collabels(none, none)  margin  ///
label msign (--) nobaselevels unstack noomitted  ///									
style(tex)  keep(*post_attack*) ///				
prehead(" \hline & \\" ///
"\multicolumn{6}{l}{\textit{Panel D: Islamic attacks; includes individual $\times$ attack FEs }} \\  " ///
"&  \\" ) ///
prefoot("  \cline{2-6} ") ///
postfoot( " \bottomrule" ///
" \end{tabular} \end{threeparttable} }\end{minipage}\end{center} \end{table*}" )

restore

****************************
**********FIGURE 3**********
****************************

*** See "Data availability statement" document ***

*All

preserve

collapse Negative Fear Anger Sadness Disgust (semean) Negative_se=Negative Fear_se=Fear Anger_se=Anger Sadness_se=Sadness Disgust_se=Disgust , by(post_attack)

foreach x of global emotions{

rename `x' mean`x'
rename `x'_se	se`x'
}

reshape long mean se, i(post_attack) j(emotion) string

gen se_up=mean+se*1.96
gen se_lo=mean-se*1.96

label define emotion 1 "Negative" 2 "Fear" 3 "Anger" 4 "Sadness" 5 "Disgust", replace

encode emotion, gen(emotion1) label(emotion)
tab emotion1
drop emotion
rename emotion1 emotion


generate emotionpost = post_attack  if emotion == 1
replace emotionpost = post_attack+3  if emotion == 2
replace emotionpost = post_attack+6  if emotion == 3
replace emotionpost = post_attack+9  if emotion == 4
replace emotionpost = post_attack+12  if emotion == 5


twoway (bar mean emotionpost if post_attack==0 , fcolor(blue%30) color(blue%50)) ///
       (bar mean emotionpost if post_attack==1, fcolor(red%50) color(red)) ///
       (rcap se_up se_lo emotionpost, lc(black) lw(medthick)) 	 , ///
       legend(row(2) order(1 "Pre-attack" 2 "Post-attack")  size(medsmall) region(style(none)) )    ///
       xlabel( 0.5 "Negative" 3.5 "Fear" 6.6 "Anger" 9.5 "Sadness" 12.5 "Disgust", noticks labsize(medsmall) labcolor(black) labgap(1) angle(0))    ///
	   ylabel(,  labsize(medsmall) labcolor(black) labgap(1) angle(0)  nogrid) xtitle("")  graphr(style(none) color(white) ) title("All attacks", color(black)) /// 
	   saving("$output/migr_1", replace)

restore

*Far-right

preserve

collapse Negative Fear Anger Sadness Disgust (semean) Negative_se=Negative Fear_se=Fear Anger_se=Anger Sadness_se=Sadness Disgust_se=Disgust , by(muslim_extremism post_attack)
keep if muslim_extremism==0

foreach x of global emotions{
rename `x' mean`x'
rename `x'_se	se`x'
}

reshape long mean se, i(post_attack) j(emotion) string
gen se_up=mean+se*1.96
gen se_lo=mean-se*1.96

label define emotion 1 "Negative" 2 "Fear" 3 "Anger" 4 "Sadness" 5 "Disgust", replace

encode emotion, gen(emotion1) label(emotion)
tab emotion1
drop emotion
rename emotion1 emotion

generate emotionpost = post_attack  if emotion == 1
replace emotionpost = post_attack+3  if emotion == 2
replace emotionpost = post_attack+6  if emotion == 3
replace emotionpost = post_attack+9  if emotion == 4
replace emotionpost = post_attack+12  if emotion == 5

twoway (bar mean emotionpost if post_attack==0 , fcolor(blue%30) color(blue%50)) ///
       (bar mean emotionpost if post_attack==1, fcolor(red%50) color(red)) ///
       (rcap se_up se_lo emotionpost, lc(black) lw(medthick)) 	 , ///
       legend(row(1) order(1 "Pre-attack" 2 "Post-attack")  size(medsmall) region(style(none)) )    ///
       xlabel( 0.5 "Negative" 3.5 "Fear" 6.6 "Anger" 9.5 "Sadness" 12.5 "Disgust", noticks labsize(medsmall) labcolor(black) labgap(1) angle(0))    ///
	   ylabel(,  labsize(medsmall) labcolor(black) labgap(1) angle(0)  nogrid) xtitle("")  graphr(style(none) color(white) ) title("Far-right attacks", color(black)) /// 
	   saving("$output/migr_1a", replace)

restore 
	  
*Islamist

preserve 

collapse Negative Fear Anger Sadness Disgust (semean) Negative_se=Negative Fear_se=Fear Anger_se=Anger Sadness_se=Sadness Disgust_se=Disgust , by(muslim_extremism post_attack)
keep if muslim_extremism==1

foreach x of global emotions{
rename `x' mean`x'
rename `x'_se	se`x'
}

reshape long mean se, i(post_attack) j(emotion) string
gen se_up=mean+se*1.96
gen se_lo=mean-se*1.96

label define emotion 1 "Negative" 2 "Fear" 3 "Anger" 4 "Sadness" 5 "Disgust", replace

encode emotion, gen(emotion1) label(emotion)
tab emotion1
drop emotion
rename emotion1 emotion

generate emotionpost = post_attack  if emotion == 1
replace emotionpost = post_attack+3  if emotion == 2
replace emotionpost = post_attack+6  if emotion == 3
replace emotionpost = post_attack+9  if emotion == 4
replace emotionpost = post_attack+12  if emotion == 5

twoway (bar mean emotionpost if post_attack==0 , fcolor(blue%30) color(blue%50)) ///
       (bar mean emotionpost if post_attack==1, fcolor(red%50) color(red)) ///
       (rcap se_up se_lo emotionpost, lc(black) lw(medthick)) 	 , ///
       legend(row(1) order(1 "Pre-attack" 2 "Post-attack")  size(medsmall) region(style(none)) )    ///
       xlabel( 0.5 "Negative" 3.5 "Fear" 6.6 "Anger" 9.5 "Sadness" 12.5 "Disgust", noticks labsize(medsmall) labcolor(black) labgap(1) angle(0))    ///
	   ylabel(,  labsize(medsmall) labcolor(black) labgap(1) angle(0)  nogrid) xtitle("")  graphr(style(none) color(white) ) title("Islamic attacks", color(black)) /// 
	   saving("$output/migr_1b", replace)
	   
grc1leg "$output/migr_1.gph" "$output/migr_1a.gph" "$output/migr_1b.gph"  , rows(2)  iscale(*.9) graphr(style(none) color(white) ) ring(3) ycommon imargin(10 10 0 5) holes(2) legendfrom("$output/migr_1a.gph")
graph export "$output/migr.pdf", replace

foreach x in 1 1a 1b{
erase "$output/migr_`x'.gph"
}
